﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.ExceptionManagement;
using Kiiro.Validation.Validators.Entities;

namespace Kiiro.Model.EventHandlers.ItemEventHandlers
{
    public class DiscussionItemEventReceiver : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            Guid siteCollectionId = new Guid();
            string serverRelativeUrl = null;
            SPListItem parentItem = null;

            ActivityHistory activity = null;            

            if (properties.ListItem[Constants.CommentFieldNames.DiscussionId] != null)
            {
                // -- Set the SRU --
                properties.ListItem[Constants.CommentFieldNames.ServerRelativeUrl] = properties.ListItem.Folder.ServerRelativeUrl;                                

                Comment comment = SPDataHelpers.MakeComment(properties.ListItem);

                // -- Set Last Comment properties in Discussion --
                properties.ListItem.Folder.ParentFolder.Item[Constants.DiscussionFieldNames.ServerRelativeUrl] = properties.ListItem.Folder.ParentFolder.ServerRelativeUrl;
                parentItem = properties.ListItem.Folder.ParentFolder.Item;
                parentItem[Constants.DiscussionFieldNames.LastCommentDate] = comment.CreatedDate;

                Discussion parentDiscussion = SPDataHelpers.MakeDiscussion(properties.ListItem.Folder.ParentFolder.Item);
                activity = PopulateCommentHistory(comment, parentDiscussion);
            }
            else
            {
                // -- Set the SRU --
                properties.ListItem[Constants.DiscussionFieldNames.ServerRelativeUrl] = properties.ListItem.Folder.ServerRelativeUrl;                

                Discussion discussion = SPDataHelpers.MakeDiscussion(properties.ListItem);
                activity = PopulateDiscussionHistory(discussion);
            }

            using (SPWeb web = properties.ListItem.ParentList.ParentWeb)
            {
                // -- Save the SRU and Last Comment properties -- 
                web.AllowUnsafeUpdates = true;
                this.DisableEventFiring();
                properties.ListItem.Update();
                if(parentItem != null)
                    parentItem.Update();
                this.EnableEventFiring();
                web.AllowUnsafeUpdates = false;

                siteCollectionId = web.Site.ID;
                serverRelativeUrl = web.ServerRelativeUrl;
            }

            if (activity != null)
            {
                activity.EventType = EventType.Created;
                ActivityHistoryModel.Save(activity, siteCollectionId, serverRelativeUrl);
            }

            base.ItemAdded(properties);
        }

        public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);
        }

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            Guid siteCollectionId = new Guid();
            string serverRelativeUrl = null;
            SPListItem parentItem = null;

            ActivityHistory activity = null;

            if (properties.ListItem[Constants.CommentFieldNames.DiscussionId] != null)
            {
                // ---- No Update Activity for Comments ----

                Comment comment = SPDataHelpers.MakeComment(properties.ListItem);
                
                // -- Set Last Comment properties in Discussion --
                parentItem = properties.ListItem.Folder.ParentFolder.Item;
                parentItem[Constants.DiscussionFieldNames.LastCommentDate] = comment.ModifiedDate;
                             
            }
            else
            {
                Discussion discussion = SPDataHelpers.MakeDiscussion(properties.ListItem);
                activity = PopulateDiscussionHistory(discussion);
            }

            using (SPWeb web = properties.ListItem.ParentList.ParentWeb)
            {
                if (parentItem != null)
                {
                    // -- Save the Last Comment properties -- 
                    web.AllowUnsafeUpdates = true;
                    this.DisableEventFiring();
                    parentItem.Update();
                    this.EnableEventFiring();
                    web.AllowUnsafeUpdates = false;
                }

                siteCollectionId = web.Site.ID;
                serverRelativeUrl = web.ServerRelativeUrl;
            }

            if (activity != null)
            {
                activity.EventType = EventType.Updated;
                ActivityHistoryModel.Save(activity, siteCollectionId, serverRelativeUrl);
            }

            base.ItemUpdated(properties);
        }

        private ActivityHistory PopulateCommentHistory(Comment comment, Discussion parentDiscussion)
        {
            ActivityHistory activity = new ActivityHistory();

            activity.CreatedBy = comment.ModifiedBy;
            activity.CreatedDate = comment.ModifiedDate;
            activity.ModifiedBy = comment.ModifiedBy;
            activity.ModifiedDate = comment.ModifiedDate;
            activity.Title = parentDiscussion.Title;

            activity.ActivityItemType = ItemType.Comment;
            activity.ActivityReferenceUrl = comment.ServerRelativeUrl;

            activity.EditType = EditType.Created;

            return activity;
        }

        private ActivityHistory PopulateDiscussionHistory(Discussion discussion)
        {
            ActivityHistory activity = new ActivityHistory();

            activity.CreatedBy = discussion.ModifiedBy;
            activity.CreatedDate = discussion.ModifiedDate;
            activity.ModifiedBy = discussion.ModifiedBy;
            activity.ModifiedDate = discussion.ModifiedDate;
            activity.Title = discussion.Title;

            activity.ActivityItemType = ItemType.Discussion;
            activity.ActivityReferenceUrl = discussion.ServerRelativeUrl;

            activity.EditType = EditType.Created;

            return activity;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/EventHandlers/ItemEventHandlers/DiscussionItemEventReceiver.cs $
 * 
 * 6     2/12/09 1:26p Shafia
 * 
 * 5     23/10/09 1:41p Shafia
 * added this line inside the "ItemAdded" when adding new comment
 * properties.ListItem.Folder.ParentFolder.Item[Constants.DiscussionFieldN
 * ames.ServerRelativeUrl] =
 * properties.ListItem.Folder.ParentFolder.ServerRelativeUrl;
 * 
 * this to set the server relative url  for the discussion because it was
 * coming back as null resulting on the comments being added not displayed
 * on the activity stream
 * 
 * 4     30/09/09 9:24a Shafia
 * 
 * 3     17/08/09 10:29a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/